package com.chinasoft.server.controller;

import com.chinasoft.server.mapper.AccountMapper;
import com.chinasoft.server.mapper.BrandMapper;
import com.chinasoft.server.pojo.Account;
import com.chinasoft.server.pojo.Brand;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController   // 将javaBean或对象转换为json字符串
@CrossOrigin  // 解决跨域问题
@AllArgsConstructor  // 通过构造方法注入属性
public class BrandController {
    // teacher 测试 不同的文件
    // 注入持久层对象

//    @Autowired
    BrandMapper brandMapper;

    // 进行全表查询
    @RequestMapping("/selectAll")
    public List<Brand> getAll() {
        return brandMapper.selectAll();
    }

    // 根据id进行删除
    @RequestMapping("/delete")   // localhost:8091/delete?id=88
    public String deleteOne(Integer id) {
        System.out.println("id is " + id);
        Integer ret = brandMapper.delete(id);
        if (ret == 1) {
            // 成功删除
            return "succ";
        } else {
            return "fail";
        }
    }

    // 根据前端的数据 插入记录
    @RequestMapping("/insert")
    // @RequestBody,  将前端的json字符串转换为 javaBean
    public String insertRec(@RequestBody Brand brand) {
        System.out.println(brand);
        Integer ret = brandMapper.insert(brand);
        if (ret == 1) {
            // 成功 insert
            return "succ";
        } else {
            return "fail";
        }
    }

    // 根据 id 进行查询接口
    @RequestMapping("/select")
    public Brand getAllById(Integer id) {
        return brandMapper.selectById(id);
    }

    // 根据id进行修改的接口
    @RequestMapping("/update")
    public String updateById(@RequestBody  Brand brand) {
        System.out.println(brand);
        Integer ret = brandMapper.update(brand);
        if (ret == 1) {
            // 成功 insert
            return "succ";
        } else {
            return "fail";
        }
    }

}
